package com.cyf.plc.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.cyf.plc.databinding.ItemRoomBinding
import com.cyf.plc.model.PlcRoom
import com.cyf.plc.viewholder.RoomViewHolder

class RoomAdapter(private val onItemClickListener: OnItemClickListener) : ListAdapter<PlcRoom, RoomViewHolder>(DIFF_CALLBACK) {

    interface OnItemClickListener {
        fun onItemClicked(plcRoom: PlcRoom, view: View)
        fun onItemLongClick(plcRoom: PlcRoom, view: View)
    }

    companion object {
        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<PlcRoom>() {

            override fun areItemsTheSame(oldItem: PlcRoom, newItem: PlcRoom): Boolean =
                oldItem.roomId == newItem.roomId

            override fun areContentsTheSame(oldItem: PlcRoom, newItem: PlcRoom): Boolean =
                oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = RoomViewHolder(
        ItemRoomBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun onBindViewHolder(holder: RoomViewHolder, position: Int) {
        holder.bind(getItem(position), onItemClickListener)
    }
}